#!/bin/sh
#
# OCF style start/stop/monitoring script for pgpool. use with heartbeat 2.0+ .
#
# Author: TANIDA Yutaka(tanida@sraoss.co.jp)
#
# Copyright (c) 2006                PgPool Global Development Group 
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of the
# author not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission. The author makes no representations about the
# suitability of this software for any purpose.  It is provided "as
# is" without express or implied warranty.
#
# 
#
# defined OCF_RESKEY_value      : none
#				  pgpool.pid file path and default port number will be read 
#  				  from this file automatically.



check_pid () {
	if [ -f $PGPOOL_PID ] 
	then
		kill -0 `cat $PGPOOL_PID` # >/dev/null 2>&1
		return $?
	else
		return 1
	fi
}



PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH
PGP_CONF=`/usr/bin/pgpool -h 2>&1 | grep default | sed -e 's/  config_file default path: //g'`

PGPOOL="/usr/bin/pgpool -f $PGP_CONF" 
PGPOOL_START_ARG=""
PGPOOL_STOP_ARG=" stop"
PGPOOL_FORCE_STOP_ARG=" -m i $PGPOOL_STOP_ARG"
PGPOOL_MONITOR=/usr/bin/pgpool.monitor 
PGPOOL_MONITOR_ARGS=--conf=$PGP_CONF localhost >/dev/null 2>&1
PGPOOL_PID=`grep logdir $PGP_CONF | tr -d "''=" | awk '{print $2."/pgpool.pid"}'`

# Source function library.
. /etc/rc.d/init.d/functions

# See how we were called.
case "$1" in
    start)
	if check_pid 
	then 
# pgpool is already running.
		exit 0
	else
		su -c "$PGPOOL $PGPOOL_START_ARG" postgres
		exit $?
    	fi
	;;
    stop)
	if check_pid
	then
		RET=`su -c "$PGPOOL $PGPOOL_STOP_ARG | grep ERROR" postgres` 
        	if [ -z "$RET" ] ; then
                	exit 0
        	else
#  try immediate stop.
	        	RET=`su -c "$PGPOOL $PGPOOL_FORCE_STOP_ARG | grep ERROR " postgres`
                	if [ -z "$RET" ] ; then
				exit 0;
			else
				exit 1;
			fi
        	fi
	else
# not running
		exit 0 
	fi
	;;
    status)
    	if check_pid
	then exit 0 # no error
	else exit 2 # not running
	fi
	;;
    monitor)
	if check_pid
	then
        	$PGPOOL_MONITOR $PGPOOL_MONITOR_ARGS
		exit $?
	else
		exit 7
	fi
	;;
    methods)
        echo start
        echo stop
        echo status
        echo methods        
        echo monitor
	echo recover
	echo reload
	;;
    recover|reload)
    	$0 stop
	$0 start
	exit 0
	;;
    meta-data)
cat <<EOF
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
  <resource-agent name="pgpool">
    <version>1.0</version>
    <longdesc lang="en">
      OCF-spec start , stop ,and monitoring script for pgpool
    </longdesc>
    <shortdesc lang="en">pgpool server control.</shortdesc>
    <parameters>
  </parameters>
  <actions>
    <action name="start"   timeout="20" />
    <action name="stop"    timeout="20" />
    <action name="status"  timeout="10" />
    <action name="monitor" depth="0"  timeout="20" interval="10" start-delay="1m" />
    <action name="recover"  timeout="20" />
    <action name="reload"  timeout="20" />
    <action name="meta-data"  timeout="5" />
  </actions>
  </resource-agent>
EOF
	exit 0;
	;;
    *)
    	echo "Usage: $0  {start|stop|status|methods|reload|recover|meta-data|monitor}"
	exit 1
esac

exit 0
